#!/bin/env python
import os
import time

import cv2
import numpy as np
import gzip

import digits_ann as ANN

model_path = 'digital_recognition_model.xml'
if not os.path.exists(model_path):
    print(f"模型 {model_path} 不存在。")
    exit(-1)

# 加载训练好的ANN模型
ann = cv2.ml.ANN_MLP_load(model_path)

# 查找测试集
tr, val, test_data = ANN.wrap_data()
te_data = list(test_data)

td_num = len(te_data)
start_time = time.time()
result = 0
for x in range(0, td_num):
  image,label,predict = ANN.test(ann, te_data[x])
  if label == predict:
    result += 1
end_time = time.time()
elapsed_time = end_time - start_time  # 计算经过的时间

print("测试次数：", td_num, "次")
print("测试耗时：", elapsed_time, "秒")
print ("测试准确率: %f" % (result / td_num))

# x = 100
# image,label,predict = ANN.test(ann, te_data[x])

# print("预测结果：")
# print(predict)
# print("实际结果：")
# print(label)

# windowname = "sample"
# cv2.namedWindow(windowname, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(windowname, image.shape[0] * 10, image.shape[1] * 10)
# cv2.imshow(windowname, image)
# cv2.waitKey(0)
# print('销毁窗口')
# cv2.destroyAllWindows()
